<template>
  <div class="login-page">
    <el-card class="box-card">
      <!-- 图片 -->
      <div class="isImage">
        <el-image
          src="http://weldon.net.cn/back/img/logo2.fc048282.png"
        ></el-image>
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 用户 -->
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="el-icon-user-solid">
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="tc">
          <el-button
            style="background-color: #409eff; color: #fff"
            @click="sign"
            >登录</el-button
          >
          <el-button type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 登录
import { loginApi } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名'
          },
          {
            min: 5,
            max: 11,
            message: '请输入5-11位字符'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码'
          },
          {
            pattern: /^\w{5,11}$/,
            message: '请输入5-11位密码'
          }
        ]
      }
    }
  },
  methods: {
    // 登录
    async sign () {
      try {
        // 校验通过
        await this.$refs.form.validate()
        const res = await loginApi(this.form)
        // 获取token
        // console.log(res.data.token)
        this.$store.commit('user/setToken', res.data.token)
        this.$router.push('/')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  background-size: cover;
  display: flex;
  .el-card {
    width: 450px;
    height: 300px;
    margin: 0 auto;
    .isImage {
      position: absolute;
      top: 50%;
      left: 46%;
      width: 130px;
      height: 130px;
      margin: 0 auto;
      margin-top: -215px;
      .el-image {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        border: 5px solid #fff;
        background-color: #eeeeee;
        box-shadow: 0 0 10px #ddd;
      }
    }
  }
  .el-form {
    padding: 0 20px;
    padding-top: 85px;
  }
  .tc {
    float: right;
  }
}
// ::v-deep .el-card__body {
// //   padding-top: 115px;
// }
</style>
